package space.yangmin.baidu.downloader.sys;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.useragent.Browser;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import space.yangmin.baidu.downloader.config.BaiduNetDiskConfig;
import space.yangmin.baidu.downloader.enums.BaiduNetdiskApiMode;
import space.yangmin.baidu.downloader.mode.AuthorizationCodeMode;
import space.yangmin.baidu.downloader.util.BrowserUtil;

/**
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/8/7/0007 8:36
 * @Author Administrator
 */
@Component
public class DownloaderApplicationRunner implements ApplicationRunner {

    @Value("${server.port}")
    private Integer serverPort;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //初始化AK
        this.initAuthorizationMode();
    }

    /**
     * 初始化AK
     */
    private void initAuthorizationMode(){
        BaiduNetDiskConfig baiduNetDiskConfig = SpringUtil.getBean(BaiduNetDiskConfig.class);
        switch (baiduNetDiskConfig.getMode()){
            //授权码
            case AUTHORIZATION_CODE:
                AuthorizationCodeMode instance = AuthorizationCodeMode.getInstance();
                BrowserUtil.open(instance.getAuthorizationUrl());
                BrowserUtil.open("http://localhost:" + serverPort);
                break;
            //简化模式
            case IMPLICIT_GRANT:
                break;
            //简化模式
            case DEVICE_CODE:
                break;
            default:
                break;
        }
    }
}
